package teaMainFrame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.text.DefaultEditorKit.InsertBreakAction;

import database.Password;
import org.omg.CORBA.PUBLIC_MEMBER;

import com.rORM.core.Query;
import com.rORM.core.QueryFactory;

import database.Score;
import database.Student;
import util.GetStuInfo;
import util.SelectTableModel;

import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JRadioButton;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;

public class SelectStuGrades extends JFrame {
	List<Student> students = new ArrayList<Student>();
	private JPanel contentPane;
	private JTable table;
	private DefaultTableModel model = new SelectTableModel();
	private int college_time;
	private String oldValue = "";
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					SelectStuGrades frame = new SelectStuGrades();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public SelectStuGrades() {
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 740, 453);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel title = new JLabel("学生成绩查询");
		title.setFont(new Font("Lucida Grande", Font.PLAIN, 17));
		title.setBounds(300, 23, 178, 39);
		contentPane.add(title);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(6, 74, 706, 299);
		contentPane.add(scrollPane);
		
		table = new JTable(model);
		scrollPane.setViewportView(table);
		model.addTableModelListener(new TableModelListener() {
			
			@Override
			public void tableChanged(TableModelEvent e) {
				
				
			}
		});
		
		
		JButton button = new JButton("返回");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				setVisible(false);
			}
		});
		button.setBounds(593, 381, 117, 29);
		contentPane.add(button);
		
		JButton button_1 = new JButton("查询");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				refresh();
				
//				new SelectStuGrades();
//				setVisible(false);
			}
		});
		button_1.setBounds(464, 381, 117, 29);
		contentPane.add(button_1);

		JRadioButton radioButton = new JRadioButton("2014");
		radioButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				college_time = 2014;
			}
		});
		radioButton.setBounds(6, 382, 64, 23);
		contentPane.add(radioButton);
		
		JRadioButton radioButton_1 = new JRadioButton("2015");
		radioButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				college_time = 2015;
			}
		});
		radioButton_1.setBounds(82, 381, 64, 23);
		contentPane.add(radioButton_1);
		
		JRadioButton radioButton_2 = new JRadioButton("2016");
		radioButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				college_time = 2016;
			}
		});
		radioButton_2.setBounds(161, 381, 64, 23);
		contentPane.add(radioButton_2);
		
		JRadioButton radioButton_3 = new JRadioButton("2017");
		radioButton_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				college_time = 2017;
			}
		});
		radioButton_3.setBounds(237, 381, 64, 23);
		contentPane.add(radioButton_3);
		
		ButtonGroup bg = new ButtonGroup();
		bg.add(radioButton);
		bg.add(radioButton_1);
		bg.add(radioButton_2);
		bg.add(radioButton_3);
		
		JButton button_2 = new JButton("删除");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Delete();
			}
		});
		button_2.setBounds(322, 381, 117, 29);
		contentPane.add(button_2);
		
	}
	
	
	public List<Student> GetStuInfo(int college_time){
		List<Student> students = new ArrayList<Student>();
		Query q = QueryFactory.createQuery();
		students = q.queryRows("select * from student where college_time = ?", Student.class, new Object[]{college_time});
		return students;
	}
	
	public void Delete(){
		if(table.getSelectedRowCount() <= 0){
			JOptionPane.showMessageDialog(null, "请选择要删除的数据行");  
			return;
		}
		int result = JOptionPane.showConfirmDialog(null, "是否确定要删除");
		if (result == JOptionPane.OK_OPTION) {  
			int stuId = Integer.valueOf(table.getValueAt(table.getSelectedRow(), 1).toString());
			if( 0 == DeleteInfo(stuId)){
				JOptionPane.showMessageDialog(null,"删除成功！","信息提示框",JOptionPane.INFORMATION_MESSAGE);
				refresh();
			}
		}
	}
	
	public int DeleteInfo(int stuId){
		Query q = QueryFactory.createQuery();
		List<Score> scores = q.queryRows("select * from score where stuid = ?",Score.class,new Object[]{stuId});
        if(scores == null) {
            int result = JOptionPane.showConfirmDialog(null, "再次确定你讲删除该人（慎重使用）");
            if (result == JOptionPane.OK_OPTION) {
                Password password = (Password) q.queryUniqueRow("select * from password where stuuser = ?", Password.class, new Object[]{stuId});

                q.delete(Password.class, password.getId());
                q.delete(Student.class, stuId);
                return 0;
            }else {
                return 1;
            }

        }
		for(Score s : scores){
            q.delete(Score.class,s.getId());
        }
//
        return 0;

	}
	
	public void refresh(){
		model.getDataVector().clear();
		students = GetStuInfo(college_time);
		GetStuInfo stuInfo = new GetStuInfo();


		for(Student s : students){
            if (stuInfo.getUniqueRowStuInfo(s) == null){
                JOptionPane.showMessageDialog(null,"无学生","信息提示框",JOptionPane.INFORMATION_MESSAGE);

            }
            else{
                model.addRow(stuInfo.getUniqueRowStuInfo(s));
            }

		}
	}

	public void update(){
		
    }
}

